<template>
  <div class="stockDeviceType">
    <div class="card-table">
      <el-button
        style="margin-bottom: 12px"
        icon="el-icon-plus"
        type="primary"
        @click="dialogVisible1 = true"
        >新增</el-button
      >
      
      <el-table
        :header-cell-style="getRowClass"
        :data="tableData"
        style="width: 100%"
        
      >
        <el-table-column align="center" type="index" label="序号" width="60">
        </el-table-column>
        <el-table-column align="center" label="产品类型" width="width">
          <template slot-scope="{ row }">
            {{ row.dates }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="unit" label="备注" width="width">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="{ row }">
            <el-button
              @click="edit(row)"
              type="text"
              >修改</el-button
            >
            <el-button
              style="margin-left: 0"
              @click="deleteOne(row)"
              type="text"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        :modal-append-to-body="false"
        title="添加型号"
        :visible.sync="dialogVisible1"
        width="30%"
      >
        <el-form
          size="small"
          ref="formCreate"
          :model="formCreate"
          label-width="80px"
          :rules="formCreateRuls"
        >
            <el-form-item label="产品类型" prop="ip">
            <el-input v-model="formCreate.ip"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible1=false">取 消</el-button>
          <el-button type="primary" @click="created()">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog
        :modal-append-to-body="false"
        title="修改规格型号"
        :visible.sync="dialogVisible2"
        width="30%"
      >
        <el-form
          size="small"
          :rules="formCreateRuls"
          :model="formCreate"
          label-width="80px"
          
        >
    <el-form-item label="产品类型" prop="ip">
            <el-input v-model="formCreate.ip"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible2=false,formCreate.ip=''">取 消</el-button>
          <el-button type="primary" @click="edits()">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>

import {
  MoldgetAll,
  Moldcreate,
  Moldupdata,
  Molddelete,
} from "@/api/stock/Mold.js";

export default {
  components: {},
  data() {
    return {
      dialogVisible1: false,
      dialogVisible2: false,
      stockDeviceTypes: [],
      formCreate: {
        ip:''
      },
      tableData: [],
      rows:{},
      formCreateRuls: {
        ip: [{ required: true, message: "请输入产品类型", trigger: "blur" }],
      }
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.getData();
    // stockDeviceTypeGet().then((res) => {
    //   console.log(222, res);
    //   this.stockDeviceTypes = res.data.data.pageData;
    // });
  },
  methods: {
    
    getData() {
      MoldgetAll().then((res) => {
        console.log(res.data.data.pageData);
        this.tableData = res.data.data.pageData;
      });
    },
    deleteOne(row) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
         const params={
          _id:row._id
         }
          Molddelete(params).then((res) => {
            if (res.data.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功",
              });
              this.getData();
            } else {
              this.$message({
                type: "warning",
                message: res.data.message,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    created() {
      const params={
        dates:this.formCreate.ip
      }
      console.log(params);
          Moldcreate(params).then((res) => {
            console.log(res);
            if (res.data.code === 200) {
              this.$message({
                type: "success",
                message: "创建成功",
              });
              this.getData();
              this.dialogVisible1 = false;
            } else {
              this.$message({
                type: "warning",
                message: res.data.message,
              });
            }
          });
        
    },
    created () {
      this.$refs.formCreate.validate((valid) => {
        if (valid) {
           const params={
        dates:this.formCreate.ip
      }
      console.log(params);
          Moldcreate(params).then((res) => {
            console.log(res);
            if (res.data.code === 200) {
              this.$message({
                type: "success",
                message: "创建成功",
              });
              this.getData();
              this.dialogVisible1 = false;
            } else {
              this.$message({
                type: "warning",
                message: res.data.message,
              });
            }
          });
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    edit(row) {
      this.formCreate.ip=row.dates
      this.rows=row;
      this.dialogVisible2=true;
    },
    edits(){
      const params ={
        dates:this.formCreate.ip,
        _id:this.rows._id
      }
      console.log(params);
 Moldupdata(params).then((res) => {
            if (res.data.code === 200) {
              this.$message({
                type: "success",
                message: "修改成功",
              });
              this.getData();
              this.dialogVisible2 = false;
              this.formCreate.ip=''
            } else {
              this.$message({
                type: "warning",
                message: res.data.message,
              });
            }
          });
    }
  },
};
</script>

<style  scoped></style>
